package ase.assignment.sts.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ase.assignment.sts.api.CommonHandler;
import ase.assignment.sts.api.HandlerFactoryProxy;
import ase.assignment.sts.utils.STSConstants;

public class LoginAction extends GenericAction {
	public static final int KEY_SUCCESS = 1;
	public static final int KEY_FAIL = 2;

	public int process(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session = request.getSession();
		String username = request.getParameter("username");
		String password = request.getParameter("pass");

		try {
			boolean isOk = HandlerFactoryProxy.instance().getCustomerHandler()
					.login(username, CommonHandler.MD5(password));

			if (isOk) {
				System.out.println("Acount valid");
				session.setAttribute(STSConstants.SESSION_USER, username);
				return KEY_SUCCESS;
			}

		} catch (Throwable e) {
			e.printStackTrace();
		}

		return KEY_FAIL;
	}
}
